package com.pingan.haofang.searchcloud.api.query.parser.dbobject.sqlnode;

import java.util.Arrays;
import java.util.List;

import com.pingan.haofang.searchcloud.api.matedata.FieldMeta;
import com.pingan.haofang.searchcloud.api.query.parser.dbobject.GroupBySupport;
import com.pingan.haofang.searchcloud.api.query.parser.dbobject.OrderBySupport;
import com.pingan.haofang.searchcloud.api.query.parser.dbobject.SelectItemSupport;
import com.pingan.haofang.searchcloud.api.query.parser.dbobject.SqlNode;
import com.pingan.haofang.searchcloud.api.query.parser.dbobject.SqlNodeType;

public class ColumnNode extends SqlNode implements GroupBySupport, OrderBySupport, SelectItemSupport {

    private String columnName;
    private String schema;
    private FieldMeta meta;
    private boolean asc = true;

    public ColumnNode() {
    }

    public ColumnNode(String columnName, String schema) {
        this.columnName = columnName;
        this.schema = schema;
    }

    public ColumnNode(String alias, String columnName, String schema) {
        this(columnName, schema);
        this.alias = alias;
    }

    @Override
    public Object getValue(Object...params) {
        throw new RuntimeException("");
    }

    @Override
    public int getType() {
        return SqlNodeType.COLUMN;
    }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public String getSchema() {
        return schema;
    }

    public void setSchema(String schema) {
        this.schema = schema;
    }

    public FieldMeta getMeta() {
        return meta;
    }

    public void setMeta(FieldMeta meta) {
        this.meta = meta;
    }

    @Override
    public int getParamCount() {
        return 0;
    }

    @Override
    public List<ColumnNode> getColumns() {
        return Arrays.asList(this);
    }

    @Override
    public String getAlias() {
        return alias == null ? getColumnName() : alias;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((columnName == null) ? 0 : columnName.hashCode());
        result = prime * result + ((schema == null) ? 0 : schema.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        ColumnNode other = (ColumnNode) obj;
        if (columnName == null) {
            if (other.columnName != null) {
                return false;
            }
        } else if (!columnName.equals(other.columnName)) {
            return false;
        }
        if (schema == null) {
            if (other.schema != null) {
                return false;
            }
        } else if (!schema.equals(other.schema)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "ColumnNode [columnName=" + columnName + ", alias=" + alias + ", schema=" + schema + "]";
    }

    public void setAsc(boolean asc) {
        this.asc = asc;
    }

    @Override
    public boolean isAsc() {
        return asc;
    }

    @Override
    public ColumnNode getColumn() {
        return this;
    }

    @Override
    public boolean exclusive() {
        return false;
    }
}
